package com.jymj.service.impl;

import com.jymj.dao.SysUserRoleDao;
import com.jymj.entity.PjProjEntity;
import com.jymj.service.SysUserRoleService;
import com.jymj.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户与角色对应关系
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年9月18日 上午9:45:48
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl implements SysUserRoleService {

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveOrUpdate(Long userId, List<Long> roleIdList) {
        if (roleIdList.isEmpty() || roleIdList.get(0)==null) {
            throw new RRException("至少选择一个权限");
        }

        //先删除用户与角色关系
        sysUserRoleDao.delete(userId);

        //保存用户与角色关系
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("roleIdList", roleIdList);
        sysUserRoleDao.save(map);
    }

    @Override
    public List<Long> queryRoleIdList(Long userId) {
        return sysUserRoleDao.queryRoleIdList(userId);
    }

    @Override
    public void delete(Long userId) {
        sysUserRoleDao.delete(userId);
    }

    @Override
    public List<PjProjEntity> queryprojId(Long userId) {
        return sysUserRoleDao.queryprojId(userId);
    }

}
